using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class COUNTRYDataAccess : Connection
    {
		#region Local Variable
        private string _strSPInsertName = "dbo.[procCOUNTRY_add]";
        private string _strSPUpdateName = "dbo.[procCOUNTRY_update]";
        private string _strSPDeleteName = "dbo.[procCOUNTRY_delete]";
        private string _strSPGetName = "dbo.[procCOUNTRY_get]";
        private string _strSPGetAllName = "dbo.[procCOUNTRY_getall]";
		private string _strSPGetPages = "dbo.[procCOUNTRY_getpaged]";
		private string _strSPIsExist = "dbo.[procCOUNTRY_isexist]";
        private string _strTableName = "[COUNTRY]";
		private string _strSPGetTransferOutName = "dbo.[procCOUNTRY_gettransferout]";
		#endregion Local Variable
		
		#region Method
        public COUNTRYInfo Get(
        String COUNTRY_CODE,
		ref string sErr)
        {
			COUNTRYInfo objEntr = new COUNTRYInfo();
			InitConnect();
			InitSPCommand(_strSPGetName);              
            AddParameter(COUNTRYInfo.Field.COUNTRY_CODE.ToString(), COUNTRY_CODE);
            
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}  
            
            if (list.Rows.Count > 0)
                objEntr = (COUNTRYInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            COUNTRYInfo result = new COUNTRYInfo();
            result.COUNTRY_CODE = (dt.Rows[i][COUNTRYInfo.Field.COUNTRY_CODE.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][COUNTRYInfo.Field.COUNTRY_CODE.ToString()]));
            result.A3 = (dt.Rows[i][COUNTRYInfo.Field.A3.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][COUNTRYInfo.Field.A3.ToString()]));
            result.COUNTRY_NAME = (dt.Rows[i][COUNTRYInfo.Field.COUNTRY_NAME.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][COUNTRYInfo.Field.COUNTRY_NAME.ToString()]));
            result.CREATEDBY = (dt.Rows[i][COUNTRYInfo.Field.CREATEDBY.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][COUNTRYInfo.Field.CREATEDBY.ToString()]));
            result.MODIFIEDBY = (dt.Rows[i][COUNTRYInfo.Field.MODIFIEDBY.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][COUNTRYInfo.Field.MODIFIEDBY.ToString()]));
            result.CREATEDDATE = (dt.Rows[i][COUNTRYInfo.Field.CREATEDDATE.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][COUNTRYInfo.Field.CREATEDDATE.ToString()]));
            result.MODIFIEDDATE = (dt.Rows[i][COUNTRYInfo.Field.MODIFIEDDATE.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][COUNTRYInfo.Field.MODIFIEDDATE.ToString()]));
           
            return result;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		/// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(COUNTRYInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);
            AddParameter(COUNTRYInfo.Field.COUNTRY_CODE.ToString(), objEntr.COUNTRY_CODE);
            AddParameter(COUNTRYInfo.Field.A3.ToString(), objEntr.A3);
            AddParameter(COUNTRYInfo.Field.COUNTRY_NAME.ToString(), objEntr.COUNTRY_NAME);
            AddParameter(COUNTRYInfo.Field.CREATEDBY.ToString(), objEntr.CREATEDBY);
            AddParameter(COUNTRYInfo.Field.MODIFIEDBY.ToString(), objEntr.MODIFIEDBY);
            AddParameter(COUNTRYInfo.Field.CREATEDDATE.ToString(), objEntr.CREATEDDATE);
            AddParameter(COUNTRYInfo.Field.MODIFIEDDATE.ToString(), objEntr.MODIFIEDDATE);
          
            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if(tmp != null && tmp != DBNull.Value)
					ret = Convert.ToInt32(tmp);
				else 
					ret=0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
			
            return ret;
        }

        public string Update(COUNTRYInfo objEntr)
        {
            openConnection();
            InitSPCommand(_strSPUpdateName);
            AddParameter(COUNTRYInfo.Field.COUNTRY_CODE.ToString(), objEntr.COUNTRY_CODE);
            AddParameter(COUNTRYInfo.Field.A3.ToString(), objEntr.A3);
            AddParameter(COUNTRYInfo.Field.COUNTRY_NAME.ToString(), objEntr.COUNTRY_NAME);
            AddParameter(COUNTRYInfo.Field.CREATEDBY.ToString(), objEntr.CREATEDBY);
            AddParameter(COUNTRYInfo.Field.MODIFIEDBY.ToString(), objEntr.MODIFIEDBY);
            AddParameter(COUNTRYInfo.Field.CREATEDDATE.ToString(), objEntr.CREATEDDATE);
            AddParameter(COUNTRYInfo.Field.MODIFIEDDATE.ToString(), objEntr.MODIFIEDDATE);
               
            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String COUNTRY_CODE
		)
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter(COUNTRYInfo.Field.COUNTRY_CODE.ToString(), COUNTRY_CODE);
              
            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }   
		
		
        
        public Boolean IsExist(
        String COUNTRY_CODE
		)
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);
            AddParameter(COUNTRYInfo.Field.COUNTRY_CODE.ToString(), COUNTRY_CODE);
              
            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if(list.Rows.Count==1)
				return true;
            return false;
        }
		
		private string CreateWhereClause(COUNTRYInfo obj)
        {
            String result = "";

            return result;
        }
        
        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
			AddParameter("DB", dtb);
			AddParameter("FROM", from);
			AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		#endregion Method
     
    }
}
